//  清除某个字符串
const clearStr = (str, oldStr, newStr) => {
  var reg = new RegExp(str, "g");
  return oldStr.replace(reg, newStr);
};

//  两个tree数据进行对比
const compute_next_step = (select_data, all_data) => {
  for (let i in all_data) { //开始遍历树
    find_select_node(all_data[i], select_data);
  }
  console.log("all data", all_data)
};
const find_select_node = (tmp_node, select_data) => {
  if (select_data.find(x => x.key === tmp_node.key)) {  //选中
    tmp_node.is_select = true;
    if (tmp_node.children) {  //当前节点选中，有子节点,开始找子节点是否选中
      judge_child_node(tmp_node.children, select_data);
    }
  } else {  //未选中
    if (tmp_node.children) {  //当前节点未选中，有子节点，开始找子节点是否选中
      let is_find = judge_child_node(tmp_node.children, select_data);
      if (is_find) {
        tmp_node.is_select = true;
      } else {
        tmp_node.is_select = false;
      }
    } else {
      tmp_node.is_select = false;
    }
  }
};
const judge_child_node = (child_list, select_data) => {
  let is_find = false;
  for (let j in child_list) {
    if (select_data.find(x => x.key === child_list[j].key)) { //子节点是否选中
      // console.log("----------", '---', child_list[j].label, '===', child_list[j].children)
      child_list[j].is_select = true;
      is_find = true;
      if (child_list[j].children) { //是否有子节点
        let my_find = judge_child_node(child_list[j].children, select_data);  //递归遍历
        // console.log("----------", my_find, '---', child_list[j].label)
        if (!my_find) {
          set_child_selected(child_list[j].children)
        }
      }
    } else {  //子节点未被选中
      if (child_list[j].children) { //是否有子节点
        let my_find = judge_child_node(child_list[j].children, select_data);
        if (my_find) {
          child_list[j].is_select = true;
          is_find = true
        } else {
          child_list[j].is_select = false;
        }
      } else {
        child_list[j].is_select = false;
      }
    }
  }
  return is_find;
};

const set_child_selected = (node, is_selected) => {
  if (Array.isArray(node) && node.length > 0) {
    node.forEach(item => {
      item.is_select = is_selected;
      if (item.children && Array.isArray(item.children)) {
        set_child_selected(item.children, is_selected)
      }
    })
  }
};

//  当前节点是否为分析大类，0:否；1：分析大类；2: 板块；3：板块下的业务；5：集团名称
//  为分析大类时，不需要显示下面的子节点；其它情况需要显示子节点
const traverse_tree1 = (arr) => {
  for (let i in arr) {
    if (arr[i].is_analysis_class == 1) {
      arr[i].isShow = true;
      if (arr[i].children) {
        traverse_tree2(arr[i]);
      }
    } else {
      arr[i].isShow = true;
      if (arr[i].children) {
        traverse_tree1(arr[i].children);
      }

    }
  }
};

const traverse_tree2 = (obj) => {
  for (let i in obj.children) {
    obj.children[i].isShow = false;
    if (obj.children[i].children) {
      traverse_tree2(obj.children[i]);
    }
  }
};

/**
 * 获取自定义选中的树key方法
 * @param arr 数据
 */
const getDefaultCheckedKeys = (arr) => {
  let retArr = [];
  for (let i in arr) {
    if (arr[i].label) {
      retArr.push({
        label: arr[i].label,
        emotion: 0
      });
    } else {
      retArr.push({
        label: arr[i].name,
        emotion: 0
      });
    }
  }
  return retArr;
};

const genResult = (node) => {
  if (!Array.isArray(node)) return []
  if (node.length <= 0) return []
  const result = []
  node.forEach(item => {
    if (item.children && Array.isArray(item.children) && item.children.length > 0) {
      const children = genResult(item.children)
      if (children.length > 0) {
        const copyItem = {...item}
        copyItem.children = children
        result.push(copyItem)
      }
    } else {
      if (item.is_select) {
        result.push({...item})
      }
    }
  })
  return result;
};

export {
  clearStr,
  compute_next_step,
  traverse_tree1,
  getDefaultCheckedKeys,
  genResult,
}
